﻿NExt.remoteSerialize = function(assembly, type) {
    var responseText;
    Ext.Ajax.request({
        url: 'next/component/' + assembly,
        success: function(response, options) {
            responseText = response.responseText;
        },
        async: false,
        headers: {
            'Post-Type': 'Instance',
            'NExt-Type': type
        },
        method: 'POST'
    });
    var object = Ext.util.JSON.decode(responseText);
    return object;
};

NExt.remoteInvoke = function(assembly, type, method, params, callback) {
    var responseText, async;
    var paramsJson = Ext.util.JSON.encode(params);
    var url = 'next/component/' + assembly;
    var ajaxMethod = 'POST';
    var headers = {
        'Post-Type': 'Method',
        'NExt-Type': type,
        'NExt-Method': method,
        'NExt-Method-Params': paramsJson
    };

    if (callback) {
        Ext.Ajax.request({
            url: url,
            success: function(response, options) {
                responseText = response.responseText;
                var object = Ext.util.JSON.decode(responseText);
                callback.success.call(callback.scope, object);
            },
            failure: function(response, options) {
                if (Ext.isFunction(callback.failure))
                    callback.failure.call(callback.scope, response, options);
            },
            async: true,
            headers: headers,
            method: ajaxMethod
        });

        return;
    }

    Ext.Ajax.request({
        url: url,
        success: function(response, options) {
            responseText = response.responseText;
        },
        async: false,
        headers: headers,
        method: ajaxMethod
    });
    var object = Ext.util.JSON.decode(responseText);
    return object;
};